local number_util = {}

local _convert_table = {
    [0] = "0",
    [1] = "1",
    [2] = "2",
    [3] = "3",
    [4] = "4",
    [5] = "5",
    [6] = "6",
    [7] = "7",
    [8] = "8",
    [9] = "9",
    [10] = "A",
    [11] = "B",
    [12] = "C",
    [13] = "D",
    [14] = "E",
    [15] = "F",
    [16] = "G"
}

local function get_num_from_char(char)
    for k, v in pairs(_convert_table) do
        if v == char then
            return k
        end
    end
    return 0
end

local function Convert(dec, x)
    local function fn(num, t)
        if (num < x) then
            table.insert(t, num)
        else
            fn(math.floor(num / x), t)
            table.insert(t, num % x)
        end
    end

    local x_t = {}
    fn(dec, x_t)

    return x_t
end

-- 10进制num转化为各种进制字符串
-- 数字转换为字符串
-- param1:要转换的数字
-- param2:要转换的进制
-- result:转换成的字符串
function number_util.dec_num_to_Xstr(dec, x)
    local x_t = Convert(dec, x)

    local text = ""
    for k, v in ipairs(x_t) do
        text = text .. _convert_table[v]
    end
    return text
end

-- 字符串逆向10进制num
-- 字符串转换为数字
-- param1:要转换的字符串
-- param2:要转换的进制
-- result:转换成的数字
function number_util.Xstr_to_dec_num(text, x)
    local x_t = {}
    local len = string.len(text)
    local index = len
    while (index > 0) do
        local char = string.sub(text, index, index)
        x_t[#x_t + 1] = get_num_from_char(char)
        index = index - 1
    end

    local num = 0
    for k, v in ipairs(x_t) do
        num = num + v * math.pow(x, k - 1)
    end
    return num
end

function number_util.mock()
    local num = 85

    print("---- dec convert to x ----\n")
    local text1 = number_util.dec_num_to_Xstr(num, 10)
    print(type(text1))
    local text2 = number_util.dec_num_to_Xstr(num, 2)
    print(type(text2))
    local text3 = number_util.dec_num_to_Xstr(num, 8)
    print(type(text3))
    local text4 = number_util.dec_num_to_Xstr(num, 16)
    print(type(text4))
    local text5 = number_util.dec_num_to_Xstr(num, 14)
    print(type(text5))
    print(string.format("dec %d to dec %s", num, text1))
    print(string.format("dec %d to bin %s", num, text2))
    print(string.format("dec %d to Oct %s", num, text3))
    print(string.format("dec %d to Hex %s", num, text4))
    print(string.format("dec %d to 14  %s", num, text5))

    print("\n---- string to num ----\n")
    local num1 = number_util.Xstr_to_dec_num(text1, 10)
    print(type(num1))
    local num2 = number_util.Xstr_to_dec_num(text2, 2)
    print(type(num2))
    local num3 = number_util.Xstr_to_dec_num(text3, 8)
    print(type(num3))
    local num4 = number_util.Xstr_to_dec_num(text4, 16)
    print(type(num4))
    local num5 = number_util.Xstr_to_dec_num(text5, 14)
    print(type(num5))
    print(string.format("dec:%s to dec %d", text1, num1))
    print(string.format("bin:%s to dec %d", text2, num2))
    print(string.format("Oct:%s to dec %d", text3, num3))
    print(string.format("Hex:%s to dec %d", text4, num4))
    print(string.format("14:%s to dec %d", text5, num5))
end

-- 测试方法
-- number_util.mock()

return number_util
